/**
 * @FileName: AboutActivity.java
 * @Package:com.bdtop.dbl.ui.activities.setting
 * @Description: TODO
 * @author: MingjunLi
 * @date:2014-9-15
 * @version V1.0
 */

package com.bdtop.dbl.ui.activities.me;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.bdtop.dbl.BaseActivity;
import com.bdtop.dbl.R;
import com.bdtop.dbl.ui.activities.WapActivity;
import com.bdtop.dbl.ui.view.TitleBar;
import com.bdtop.dbl.utils.Constants;
import com.bdtop.dbl.utils.Preferences;
import com.bdtop.dbl.utils.UpdateService;

/**
 * @ClassName: AboutActivity
 * @Description: TODO
 * @author: MingjunLi
 * @date:2014-9-15
 */
public class AboutActivity extends BaseActivity implements OnClickListener {

    private TitleBar mTitleBar;
    private View aboutSoftware, operGuide, versionUpdate, customerService, copyright;
    private TextView version;
    private boolean isUpdate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);

        initViews();
    }

    public static final void launch(Context context) {
        Intent intent = new Intent(context, AboutActivity.class);
        context.startActivity(intent);
    }

    private void initViews() {

        mTitleBar = (TitleBar) findViewById(R.id.title_bar);
        mTitleBar.setTitle(R.string.about);
        mTitleBar.setBelongActivity(this);

        aboutSoftware = findViewById(R.id.layout_about_software);
        operGuide = findViewById(R.id.layout_oper_guide);
        versionUpdate = findViewById(R.id.layout_version_update);
        customerService = findViewById(R.id.layout_contact_customer_service);
        copyright = findViewById(R.id.layout_copyright);
        version = (TextView) findViewById(R.id.version);
        PackageInfo info = null;
        try {
            info = getPackageManager().getPackageInfo(getPackageName(), 0);
            version.setText("V" + info.versionName);

            float ver = Float.valueOf(info.versionName);
            int version = (int) (ver * 100);
            if (Integer.parseInt(Preferences.getVersion(this)) > version) {
                isUpdate = true;
            }
        } catch (NameNotFoundException e) {
        }

        aboutSoftware.setOnClickListener(this);
        operGuide.setOnClickListener(this);
        versionUpdate.setOnClickListener(this);
        customerService.setOnClickListener(this);
        copyright.setOnClickListener(this);
    }
    
    private void showUpdateDialog() {
        final Dialog dialog = new Dialog(this, R.style.Theme_Light_NoTitle_Dialog);
        View content = LayoutInflater.from(this).inflate(R.layout.dialog_confirm_2, null);

        TextView txMessage = (TextView) content.findViewById(R.id.tx_message);
        txMessage.setText(R.string.update_msg);

        OnClickListener listener = new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                dialog.dismiss();

                if (arg0.getId() == R.id.btn_confirm) {
                    Intent intent = new Intent(AboutActivity.this,
                            UpdateService.class);
                    intent.putExtra("Key_App_Name", getString(R.string.app_name));
                    intent.putExtra("Key_Down_Url",
                            Preferences.getDownurl(AboutActivity.this));
                    startService(intent);
                   }
            }
        };

        Button btnCon=(Button) content.findViewById(R.id.btn_confirm);
        btnCon.setOnClickListener(listener);
        btnCon.setText(R.string.just_update);
        Button btnCan=(Button) content.findViewById(R.id.btn_cancel);
        btnCan.setOnClickListener(listener);
        btnCan.setText(R.string.not_update);

        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(content);
        dialog.show();
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_about_software:
//                AboutSoftActivity.launch(this);
            	final String url="http://www.hbmat.cn/bdtop/about.html";
            	WapActivity.launch(this, url , getString(R.string.about));
                break;
            case R.id.layout_oper_guide:
            	WapActivity.launch(this, Constants.url_help, getString(R.string.oper_guide));
                break;
            case R.id.layout_version_update:
                if (!isUpdate) {
                    showToast(R.string.latest_version);
                } else {
                    showUpdateDialog();
                }

                break;
            case R.id.layout_contact_customer_service:
                AboutServicesActivity.launch(this);
                break;
            case R.id.layout_copyright:
                AboutCopyRightActivity.launch(this);
                break;

            default:
                break;
        }
    }
}
